/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author My
 */
public class BookDAO implements IBookDAO {

    DataSource source = null;

    public BookDAO() throws NamingException, Exception {
        if (source != null) {
            return;
        }
        try {
            Context ic = new InitialContext();
            source = (DataSource) ic.lookup("jdbc/sociallibraryConnection");
        } catch (NamingException ex) {
            throw ex;
        }
        if (source == null) {
            throw new Exception("Lookup failed");
        }
    }
    
    

    @Override
    public Book getByISBN(String isbn) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void save(Book book) {
        Connection conn = null;

        StringBuilder query = new StringBuilder();
        query.append("INSERT INTO book (isbn, title, description, page_number, image, regist_date, moder_status) ");
        query.append("VALUES(");
        query.append("?, ?, ?, ?, ?,"); //isbn, title, description, page_number, image
        query.append("TO_DATE(sysdate, 'DD-MM-YYYY'),"); //regist_date
        query.append("(SELECT id FROM moder_status WHERE status = 'new'))");//moder_status

        try {
            conn = getConnection(source);
            PreparedStatement preparedStatement = conn.prepareStatement(query.toString());
            preparedStatement.setString(1, book.getISBN());
            preparedStatement.setString(2, book.getTitle());
            preparedStatement.setString(3, book.getDescription());
            preparedStatement.setInt(4, book.getPageNumber());
            preparedStatement.setString(5, book.getImageURL());

            preparedStatement.execute();

        } catch (SQLException ex) {
            try {
                throw new Exception(ex.fillInStackTrace());
            } catch (Exception ex1) {
                Logger.getLogger(BookDAO.class.getName()).log(Level.SEVERE, null, ex1);
            }
        } finally {
            if (conn != null) {
                releaseConnection(conn);
            }
        }
    }

    @Override
    public void delete(Book t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Book t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Connection getConnection(DataSource source) {
        try {
            Connection conn = source.getConnection();
            conn.setAutoCommit(false);
            return conn;

        } catch (SQLException ex) {
            return null;
        }
    }

    @Override
    public void releaseConnection(Connection connection){
        try {
            connection.close();
        } catch (SQLException ex) {
        }
    }
    
    
}
